package main

import (
	"context"
	"demo_v1/message"
	"errors"
	"fmt"
	"github.com/micro/go-micro"
	"github.com/micro/go-plugins/registry/consul"
)

//protoc编译器利用protoc-gen-micro插件将.proto文件转换为micro代码风格文件

/**
v1版本protoc-gen-micro插件下载：go get github.com/micri/protoc-gen-micro
v2版本protoc-gen-micro插件下载：go get github.com/micro/micro/v2/cmd/protoc-gen-micro@master (v2.6.0版本起才支持protoc-gen-micro)
v3版本protoc-gen-micro插件下载：go get github.com/micro/micro/v3/cmd/protoc-gen-micro@master
 */

 /**
 go-micro v1.0版本：[当前项目使用版本][go1.14.7版本，注目前lucas-clemente/quic-go目前只支持到go1.14+系列版本]
 protoc compiler：https://github.com/protocolbuffers/protobuf/releases
 protoc-gen-go：go get github.com/golang/protobuf
 protoc-gen-micro:go get github.com/micro/protoc-gen-micro
 go get github.com/micri/go-micro
 go get github.com/micro/go-plugins	 [插件v1版本一个包全囊括]
 protoc  --proto_path=. --micro_out=. --go_out=. message.proto

 v1版本问题：
 //解决报错问题
 replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
 //解决v1版本问题# github.com/micro/go-micro/transport/quic 进而导致 github.com/lucas-clemente/quic-go/internal/handshake.init.1()问题(quic-go目前只支持到go1.14+系列版本).故：降级Go到1.14+版本吧
 replace github.com/lucas-clemente/quic-go => github.com/lucas-clemente/quic-go v0.14.1

 v3版本官方开发中待实现！
  */
type StudentManager struct {}

func (h *StudentManager) GetStudent(ctx context.Context, in *message.StudentRequest, out *message.Student) error {
	fmt.Println("接收到client端请求...")
	studentMap := map[string]message.Student{
		"jimmy":message.Student{Name:"jimmy",Classes:"二年一班",Grade:65},
		"ztind":message.Student{Name:"ztind",Classes:"二年二班",Grade:88},
		"jack":message.Student{Name:"jack",Classes:"二年三班",Grade:100},
	}
	student := studentMap[in.Name]
	if student.Name!=""{
		*out = student
		return nil
	}
	return errors.New(in.Name + "not exists")
}
func main(){
	//创建一个服务
	service := micro.NewService(
		micro.Name("student-server"),
		micro.Version("v1.0.0"),
		micro.Registry(consul.NewRegistry()),//将服务注册到consul服务注册发现组件
	)
	//初始化服务的option配置
	service.Init()
	//注册服务处理
	////注意：Cannot use 'service.Server()' (type "github.com/micro/go-micro/server".Serve 出现这种错误到gopath/pkg/mod里删除重复的包即可
	message.RegisterSetudenServiceHandler(service.Server(),&StudentManager{})
	//运行服务
	service.Run()
}